﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VERKProjectHopur16.Models;
using VERKProjectHopur16.Helpers;


namespace VERKProjectHopur16.Controllers
{
    /// <summary>
    /// Controller for contents; New, articles, papers, researches and RR.
    /// </summary>
    [HandleError]
    public class ContentController : ParentController
    {

        IDataRepository m_rep = null;

        public ContentController()
        {
            m_rep = new DataRepository();
        }

        public ContentController(IDataRepository rep)
        {
            m_rep = rep;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SingleContent(int? id)
        {
            if (id.HasValue)
            {
                var real_id = id.Value;
                var result = m_rep.GetContentById(real_id);
                if (result == null)
                {
                    return View("NotFound");
                }
                return View(result);
            }
            else
            {
                return View("NotFound");
            }
        }

        public ActionResult AllNews()
        {
            IEnumerable<Content> allNews;
            IEnumerable<Content> AllNewsMed;
            IEnumerable<Content> AllNewsPsi;
            IEnumerable<Content> AllNewsSoc;
            const int ALL_LANGUAGES = 4;
            int lang = ALL_LANGUAGES;
            String strSort = "InsertDate";

            if ( Session["langNews"] != null )
            {
                Int32.TryParse( Session["langNews"].ToString( ), out lang );
            }

            if( Session["orderNews"] != null )
            {
                if ( Session["orderNews"].Equals(1) )
                {
                    strSort = "InsertDate";
                }
                else if ( Session["orderNews"].Equals(2) )
                {
                    strSort = "Title";
                }
                else if ( Session["orderNews"].Equals(3) )
                {
                    strSort = "Author.Name";
                }
            }

            // All News:
            var tempResult = (from content in m_rep.GetAllContent()
                              where content.CategoryID == 1
                              && content.Active == true
                              select content);
            if (lang != ALL_LANGUAGES)
            {
                tempResult = tempResult.Where(m => m.LanguageID == lang);
            }
            if (strSort == "InsertDate")
            {
                allNews = tempResult.AsQueryable().OrderByDescending(strSort);
                // News filtered by department:
                AllNewsMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderByDescending(strSort);
                AllNewsPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderByDescending(strSort);
                AllNewsSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderByDescending(strSort);
            }
            else
            { 
                allNews = tempResult.AsQueryable().OrderBy(strSort);
                // News filtered by department:
                AllNewsMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderBy(strSort);
                AllNewsPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderBy(strSort);
                AllNewsSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderBy(strSort);
            }

            DepViewModel model = new DepViewModel();
            model.allDept = allNews;
            model.medDept = AllNewsMed;
            model.psiDept = AllNewsPsi;
            model.socDept = AllNewsSoc;
            return View(model);
        }


        public ActionResult AllArticles()
        {
            IEnumerable<Content> AllArt;
            IEnumerable<Content> AllArtMed;
            IEnumerable<Content> AllArtPsi;
            IEnumerable<Content> AllArtSoc;
            const int ALL_LANGUAGES = 4;

            int lang = ALL_LANGUAGES;
            String strSort = "InsertDate";

            if (Session["langArt"] != null)
            {
                Int32.TryParse(Session["langArt"].ToString(), out lang);
            }

            if (Session["orderArticles"] != null)
            {
                if (Session["orderArticles"].Equals(1))
                {
                    strSort = "InsertDate";
                }
                else if (Session["orderArticles"].Equals(2))
                {
                    strSort = "Title";
                }
                else if (Session["orderArticles"].Equals(3))
                {
                    strSort = "Author";
                }
            }

            // All Articles:
            var tempResult = (from content in m_rep.GetAllContent()
                              where content.CategoryID == 2
                              && content.Active == true
                              select content);
            if (lang != ALL_LANGUAGES)
            {
                tempResult = tempResult.Where(m => m.LanguageID == lang);
            }

            if (strSort == "InsertDate")
            {
                AllArt = tempResult.AsQueryable().OrderByDescending(strSort);
                // Articles filtered by department:
                AllArtMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderByDescending(strSort);
                AllArtPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderByDescending(strSort);
                AllArtSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderByDescending(strSort);
            }
            else
            {
                AllArt = tempResult.AsQueryable().OrderBy(strSort);
                // Articles filtered by department:
                AllArtMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderBy(strSort);
                AllArtPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderBy(strSort);
                AllArtSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderBy(strSort);
            }

            DepViewModel model = new DepViewModel();
            model.allDept = AllArt;
            model.medDept = AllArtMed;
            model.psiDept = AllArtPsi;
            model.socDept = AllArtSoc;
            return View(model);
        }
            

        public ActionResult AllPapers()
        {
            IEnumerable<Content> AllPap;
            IEnumerable<Content> AllPapMed;
            IEnumerable<Content> AllPapPsi;
            IEnumerable<Content> AllPapSoc;
            const int ALL_LANGUAGES = 4;

            int lang = ALL_LANGUAGES;
            String strSort = "InsertDate";

            if (Session["langPapers"] != null)
            {
                Int32.TryParse(Session["langPapers"].ToString(), out lang);
            }

            if (Session["orderPapers"] != null)
            {
                if (Session["orderPapers"].Equals(1))
                {
                    strSort = "InsertDate";
                }
                else if (Session["orderPapers"].Equals(2))
                {
                    strSort = "Title";
                }
                else if (Session["orderPapers"].Equals(3))
                {
                    strSort = "Author";
                }
            }

            // All Papers:
            var tempResult = (from content in m_rep.GetAllContent()
                              where content.CategoryID == 4
                              && content.Active == true
                              select content);
            if (lang != ALL_LANGUAGES)
            {
                tempResult = tempResult.Where(m => m.LanguageID == lang);
            }

            if (strSort == "InsertDate")
            {
                AllPap = tempResult.AsQueryable().OrderByDescending(strSort);
                // Papers filtered by department:
                AllPapMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderByDescending(strSort);
                AllPapPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderByDescending(strSort);
                AllPapSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderByDescending(strSort);
            }
            else
            {
                AllPap = tempResult.AsQueryable().OrderBy(strSort);
                // Papers filtered by department:
                AllPapMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderBy(strSort);
                AllPapPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderBy(strSort);
                AllPapSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderBy(strSort);
            }

            DepViewModel model = new DepViewModel();
            model.allDept = AllPap;
            model.medDept = AllPapMed;
            model.psiDept = AllPapPsi;
            model.socDept = AllPapSoc;
            return View(model);
        }

        public ActionResult AllResearches()
        {
            IEnumerable<Content> AllRes;
            IEnumerable<Content> AllResMed;
            IEnumerable<Content> AllResPsi;
            IEnumerable<Content> AllResSoc;
            const int ALL_LANGUAGES = 4;

            int lang = ALL_LANGUAGES;
            String strSort = "InsertDate";

            if (Session["langResearches"] != null)
            {
                Int32.TryParse(Session["langResearches"].ToString(), out lang);
            }

            if (Session["orderResearches"] != null)
            {
                if (Session["orderResearches"].Equals(1))
                {
                    strSort = "InsertDate";
                }
                else if (Session["orderResearches"].Equals(2))
                {
                    strSort = "Title";
                }
                else if (Session["orderResearches"].Equals(3))
                {
                    strSort = "Author";
                }
            }

            // All Researches:
            var tempResult = (from content in m_rep.GetAllContent()
                              where content.CategoryID == 5
                              && content.Active == true
                              select content);
            if (lang != ALL_LANGUAGES)
            {
                tempResult = tempResult.Where(m => m.LanguageID == lang);
            }

            if (strSort == "InsertDate")
            {
                AllRes = tempResult.AsQueryable().OrderByDescending(strSort);
                // Researches filtered by department:
                AllResMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderByDescending(strSort);
                AllResPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderByDescending(strSort);
                AllResSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderByDescending(strSort);
            }
            else
            {
                AllRes = tempResult.AsQueryable().OrderBy(strSort);
                // Researches filtered by department:
                AllResMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderBy(strSort);
                AllResPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderBy(strSort);
                AllResSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderBy(strSort);
            }

            DepViewModel model = new DepViewModel();
            model.allDept = AllRes;
            model.medDept = AllResMed;
            model.psiDept = AllResPsi;
            model.socDept = AllResSoc;
            return View(model);
        }

        public ActionResult AllRightsAndRegulations()
        {
            IEnumerable<Content> AllRr;
            IEnumerable<Content> AllRrMed;
            IEnumerable<Content> AllRrPsi;
            IEnumerable<Content> AllRrSoc;
            const int ALL_LANGUAGES = 4;

            int lang = ALL_LANGUAGES;
            String strSort = "InsertDate";

            if (Session["langRR"] != null)
            {
                Int32.TryParse(Session["langRR"].ToString(), out lang);
            }

            if (Session["orderRR"] != null)
            {
                if (Session["orderRR"].Equals(1))
                {
                    strSort = "InsertDate";
                }
                else if (Session["orderRR"].Equals(2))
                {
                    strSort = "Title";
                }
                else if (Session["orderRR"].Equals(3))
                {
                    strSort = "Author";
                }
            }

            // All RR:
            var tempResult = (from content in m_rep.GetAllContent()
                              where content.CategoryID == 3
                              && content.Active == true
                              select content);
            if (lang != ALL_LANGUAGES)
            {
                tempResult = tempResult.Where(m => m.LanguageID == lang);
            }

            if (strSort == "InsertDate")
            {
                AllRr = tempResult.AsQueryable().OrderByDescending(strSort);
                // RR filtered by department:
                AllRrMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderByDescending(strSort);
                AllRrPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderByDescending(strSort);
                AllRrSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderByDescending(strSort);
            }
            else
            {
                AllRr = tempResult.AsQueryable().OrderBy(strSort);
                // RR filtered by department:
                AllRrMed = tempResult.Where(m => m.DepartmentID == 2).AsQueryable().OrderBy(strSort);
                AllRrPsi = tempResult.Where(m => m.DepartmentID == 3).AsQueryable().OrderBy(strSort);
                AllRrSoc = tempResult.Where(m => m.DepartmentID == 4).AsQueryable().OrderBy(strSort);
            }

            DepViewModel model = new DepViewModel();
            model.allDept = AllRr;
            model.medDept = AllRrMed;
            model.psiDept = AllRrPsi;
            model.socDept = AllRrSoc;
            return View(model);
        }

        /// <summary>
        /// Helping function for language selection
        /// </summary>
        public ActionResult LanguageNews(int? id)
        {
            if (id.HasValue)
            {
                Session["langNews"] = id;
            }
            return RedirectToAction("AllNews");
        }
        /// <summary>
        /// Helping function to order news
        /// </summary>
        public ActionResult OrderNews(int? id)
        {
            if (id.HasValue)
            {
                Session["orderNews"] = id;
            }
            return RedirectToAction("AllNews");
        }
        /// <summary>
        /// Helping function for language selection
        /// </summary>
        public ActionResult LanguageArticles(int? id)
        {
            if (id.HasValue)
            {
                Session["langArt"] = id;
            }
            return RedirectToAction("AllArticles");
        }

        /// <summary>
        /// Helping function tp order Articles
        /// </summary>
        public ActionResult OrderArticle(int? id)
        {
            if (id.HasValue)
            {
                Session["orderArticles"] = id;
            }
            return RedirectToAction("AllArticles");
        }

        /// <summary>
        /// Helping function for language selection
        /// </summary>
        public ActionResult LanguagePapers(int? id)
        {
            if (id.HasValue)
            {
                Session["langPaper"] = id;
            }
            return RedirectToAction("AllPapers");
        }

        /// <summary>
        /// Helping function to order Papers
        /// </summary>
        public ActionResult OrderPapers(int? id)
        {
            if (id.HasValue)
            {
                Session["orderPapers"] = id;
            }
            return RedirectToAction("AllPapers");
        }

        /// <summary>
        /// Helping function for language selection
        /// </summary>
        public ActionResult LanguageResearches(int? id)
        {
            if (id.HasValue)
            {
                Session["langResearch"] = id;
            }
            return RedirectToAction("AllResearches");
        }

        /// <summary>
        /// Helping function to order Researches
        /// </summary>
        public ActionResult OrderResearches(int? id)
        {
            if (id.HasValue)
            {
                Session["orderResearches"] = id;
            }
            return RedirectToAction("AllResearches");
        }

        /// <summary>
        /// Helping function for language selection
        /// </summary>
        public ActionResult LanguageRR(int? id)
        {
            if (id.HasValue)
            {
                Session["langRR"] = id;
            }
            return RedirectToAction("AllRightsAndRegulations");
        }

        /// <summary>
        /// Helping function for to order RR
        /// </summary>
        public ActionResult OrderRR(int? id)
        {
            if (id.HasValue)
            {
                Session["orderRR"] = id;
            }
            return RedirectToAction("AllRightsAndRegulations");
        }
    }
}
